package com.software.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.software.broadcast.receiver.NetworkReceiver;
import com.software.broadcast.receiver.NormalAReceiver;
import com.software.broadcast.receiver.NormalBReceiver;

public class MainActivity extends AppCompatActivity {

    private Button btn_unregister;
    private Button btn_register;
    private NormalAReceiver aReceiver;
    private NormalBReceiver bReceiver;
    private Button btn_send;
    private Button btn_send_order;
    private Button btn_shock;
    private NetworkReceiver networkReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvents();
    }

    private void initEvents() {
        btn_register.setOnClickListener(v -> {
            // 在代码中进行的注册， 动态注册
            aReceiver = new NormalAReceiver();
            IntentFilter aFilter = new IntentFilter("com.software.broadcast.action");
            // IntentFilter用于识别哪一类广播
            aFilter.setPriority(8);

            registerReceiver(aReceiver, aFilter);

            bReceiver = new NormalBReceiver();
            IntentFilter bFilter = new IntentFilter("com.software.broadcast.action");
            bFilter.setPriority(10);

            registerReceiver(bReceiver, bFilter);
        });

        btn_unregister.setOnClickListener(v -> {
            unregisterReceiver(aReceiver);
            unregisterReceiver(bReceiver);

        });

        btn_send.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction("com.software.broadcast.action");
            intent.putExtra("action", "zhe");
            sendBroadcast(intent);
        });

        btn_send_order.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction("com.software.broadcast.action");
            intent.putExtra("action", "zhe");
            // 发送有序广播
            sendOrderedBroadcast(intent, null);
        });

        btn_shock.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction("com.software.broadcast.receiver.ShockReceiver.action");
            // 广播的类的全路径
            ComponentName componentName = new ComponentName(MainActivity.this, "com.software.broadcast.receiver.ShockReceiver");
            intent.setComponent(componentName);
            sendBroadcast(intent);
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 注册广播
        networkReceiver = new NetworkReceiver();
        IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(networkReceiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(networkReceiver);
    }

    private void initViews() {
        btn_register = findViewById(R.id.btn_register);
        btn_unregister = findViewById(R.id.btn_unregister);

        btn_send = findViewById(R.id.btn_send);

        btn_send_order = findViewById(R.id.btn_send_order);
        btn_shock = findViewById(R.id.btn_shock);
    }
}